﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace eTraining
{
    public class ButtonBoxTool
    {
        private List<Button> buttons = new List<Button>();

        public void Draw(Graphics g, TopPanel pnl)
        {
            Point pt = GetMousePoint(pnl);

            g.SmoothingMode = SmoothingMode.HighQuality;

            foreach (Button itm in buttons)
            {
                DrawButton(g, itm, pnl,pt);
            }

            g.SmoothingMode = SmoothingMode.Default;
        }

        public ButtonBoxTool()
        {
            int xOffset = 2;

            Button btn = new Button();
            btn.ImageIndex = 0;
            btn.Bounds = new Rectangle(xOffset, 8, 24, 24);
            buttons.Add(btn);

            xOffset += btn.Bounds.Width + 2;
            btn = new Button();
            btn.ImageIndex = 1;
            btn.Bounds = new Rectangle(xOffset, 8, 24, 24);
            buttons.Add(btn);

            xOffset += btn.Bounds.Width + 2;
            btn = new Button();
            btn.ImageIndex = 2;
            btn.Bounds = new Rectangle(xOffset, 8, 24, 24);
            buttons.Add(btn);

            xOffset += btn.Bounds.Width + 2;
            btn = new Button();
            btn.ImageIndex = 3;
            btn.Bounds = new Rectangle(xOffset, 8, 24, 24);
            buttons.Add(btn);

            xOffset += btn.Bounds.Width + 2;
            btn = new Button();
            btn.ImageIndex = -1;
            btn.Bounds = new Rectangle(xOffset, 8, 2, 24);
            buttons.Add(btn);

            xOffset += btn.Bounds.Width + 2;
            btn = new Button();
            btn.ImageIndex = 4;
            btn.Text = "个人信息管理";
            btn.Bounds = new Rectangle(xOffset, 8, 104, 24);
            buttons.Add(btn);

            //xOffset += btn.Bounds.Width + 2;
            //btn = new Button();
            //btn.ImageIndex = 4;
            //btn.Text = "个人信息管理";
            //btn.Bounds = new Rectangle(xOffset, 8, 104, 24);
            //buttons.Add(btn);

        }

        private void DrawButton(Graphics g,Button btn,TopPanel pnl,Point pt)
        {
            if (btn.ImageIndex == -1)
            {
                g.DrawLine(Pens.DimGray, btn.Bounds.Left, btn.Bounds.Top + 4, btn.Bounds.Left, btn.Bounds.Bottom - 4);
                g.DrawLine(Pens.Silver, btn.Bounds.Left + 1, btn.Bounds.Top + 4, btn.Bounds.Left + 1, btn.Bounds.Bottom - 4);
                return;
            }
            
            Image img= pnl.ImgList.Images[btn.ImageIndex];
            int imgYOffset = (btn.Bounds.Height-img.Size.Height)/2;
            int imgXOffset = imgYOffset;


            if (btn.Bounds.Contains(pt) == true)
            {
                GraphicsPath gp = StyleHelper.GetRoundedRectPath(btn.Bounds,4);
                g.FillPath(Brushes.LightBlue, gp);
                g.DrawPath(Pens.Black, gp);
            }

            g.DrawImage(img, btn.Bounds.Left + imgXOffset, btn.Bounds.Top + imgYOffset);

            if (btn.Text != null && btn.Text.Trim().Length != 0)
            {
                g.DrawString(
                    btn.Text, 
                    pnl.Font, 
                    Brushes.Black, 
                    btn.Bounds.Left + imgXOffset * 2 + img.Width,
                    btn.Bounds.Top + imgYOffset + 2
                );
            }
        }

        private Point GetMousePoint(TopPanel pnl)
        {
            Point rst = pnl.PointToClient(Control.MousePosition);
            return rst;
        }

        public int HitTest(TopPanel pnl)
        {
            int rst = -1;
            Point pt = GetMousePoint(pnl);

            Rectangle rect = Rectangle.Empty;

            foreach (Button itm in buttons)
            {
                if (itm.Bounds.Contains(pt) == true)
                {
                    return itm.ImageIndex;
                }
            }

            return rst;
        }

        private class Button
        {
            public int ImageIndex { get; set; }
            public string Text { get; set; }
            public Rectangle Bounds { get; set; }
        }
    }
}
